<?php

namespace app\admin\server\user;

use app\admin\model\music\MusicModel;
use app\admin\model\user\UserTypeModel;
use app\admin\ServerBase;
use think\db\exception\DbException;
use think\Request;

class UserTypeServer extends ServerBase
{
    function __construct( Request $request)
    {
        //$this->checkAuth();
        $this->request=$request;
        $this->param=$request->param();
        $this->model=new UserTypeModel();
    }

    function getList(){
        [$page,$limit] = $this->getPage($this->param);
        [$list,$count] = $this->model->getList($this->request,[],'*',$page,$limit);
        $result['data']=$list;
        $result['count']=$count;
        $this->getMessageCode('adminMessage.success.select',2);
        return $result;
    }

    function getDetail(){
        $info = $this->model->getFindById($this->request->param('id'));
        $this->getMessageCode('adminMessage.success.select',2);
        return $info;
    }
    function add(){
        $post = $this->request->post();

        try {
            $this->model->save($post);
            $this->getMessageCode('adminMessage.success.insert',2);
            return true;
        }catch (\Error $error){
            $this->ERROR = $error->getMessage();
            return false;
        }catch (DbException $dbException){
            $this->ERROR = $dbException->getMessage();
            return false;
        }
    }
    function edit(){
        $id = $this->request->param('id',0);
        $post = $this->request->post();
        $post['id'] = $id;
        $info  = $this->model->getFindById($id);
        if(empty($info)){
            $this->getMessageCode('adminMessage.error.update',1);
            return false;
        }
        if($info->save($post)){
            $this->getMessageCode('adminMessage.success.update',2);
            return true;
        }else{
            $this->getMessageCode('adminMessage.error.update',1);
            return false;
        }
    }
}